home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / server / src / ping.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  4.5 KB  |  200 lines

  1. #include "headers.h" /* has all the important stuff */
  2.  
  3. /* this file has function to implement pings.. for timeouts */
  4.  
  5. /* for child and start pinging */
  6. void startPings()
  7. {
  8.    int res;
  9.  
  10.    debug("starting the ping/pong timeout procedure..\n\n");
  11.    debug("before forking, curPid = %d\n", curPid);
  12.  
  13.    curPid++;
  14.    clients[curClient].pinging = 1;
  15.  
  16.    res = fork();
  17.    if (res == ERROR)
  18.    {
  19.       error("error forking: %s\n\n", strerror(errno));
  20.       quit(ERROR);
  21.    } 
  22.  
  23.    else if (res == 0)
  24.    { 
  25.       if (curPid >= MAXNUMKIDS)
  26.       {
  27.          error("too many processes for the client..\n\n");
  28.  
  29.          res = seteuid(0); 
  30.          if (res == ERROR) 
  31.          {
  32.             error("error with seteuid: %s\n\n", strerror(errno));
  33.             quit(ERROR);
  34.          }
  35.  
  36.          res = kill(getppid(), SIGTERM);
  37.          if (res == ERROR) 
  38.             error("error killing parent pid %d: %s\n\n", 
  39.                   getppid(), strerror(errno));
  40.  
  41.          res = seteuid(pwd->pw_uid);
  42.          if (res == ERROR) 
  43.          {
  44.             error("error with seteuid: %s\n\n", strerror(errno));
  45.             quit(ERROR);
  46.          }
  47.  
  48.          quit(ERROR);
  49.       }
  50.  
  51.       clients[curClient].free    = ERROR; /* we're used */
  52.       clients[curClient].running = 1; 
  53.  
  54.       clients[curClient].newData = 0; /* no new data yet */
  55.  
  56.       clients[curClient].pidstats[curPid].pid  = getpid(); 
  57.       clients[curClient].pidstats[curPid].ppid = getppid();
  58.  
  59.       child = 1;
  60.  
  61.       /* check for new data.. according to the parent */
  62.       signal(SIGUSR1, gotNewData); 
  63.       sendPings(); /* child loops endlessly with pings */
  64.    }
  65.  
  66.    else
  67.    {
  68.       if (curPid >= MAXNUMKIDS) 
  69.       {
  70.          error("too many children for client %04d.. aborting\n\n",
  71.                clients[curClient].ID);
  72.  
  73.          res = wait((int *)NULL);
  74.          if (res == ERROR)
  75.          {
  76.             error("error wait()'ing for child: %s\n\n", 
  77.                   strerror(errno));
  78.  
  79.             quit(ERROR);
  80.          } 
  81.  
  82.          else curPid--; /* the child will take care of this */
  83.       }
  84.  
  85.       clients[curClient].pidstats[curPid].pid = res;
  86.       clients[curClient].running = 1;
  87.  
  88.       child = 1;
  89.       signal(SIGUSR2, startUp); /* used when parent can continue */
  90.    }
  91. }
  92.  
  93.  
  94. /* ------------------------------------------ */
  95.  
  96.  
  97. /* send pings to client */
  98. void sendPings()
  99. {
  100.    int count = 0;   /* prevent overflows */
  101.  
  102.    int res;     /* results from various functions */
  103.    int pingval; /* random value sent as ping to client */
  104.  
  105.    char *pongptr, *dataptr;
  106.  
  107.    char pongstr[8];
  108.    char readbuf[MAXREADSIZE];
  109.  
  110.    checkValid();
  111.  
  112.    debug("(in sendPings) ready to ping..\n\n");
  113.  
  114.    while(1)
  115.    {
  116.       int sleeptime = MAXTIMEOUT;
  117.  
  118.       while(1)
  119.       {
  120.          int ret;
  121.          ret = sleep(sleeptime);
  122.  
  123.          if ((ret != 0) && (clients[curClient].newData != 1))
  124.             sleeptime = ret;
  125.  
  126.          else break;
  127.       }
  128.  
  129.       pingval = ((int)random() % 9999) + 1;
  130.       send_data((clients[curClient]).sockfd, "PING %d", pingval);
  131.  
  132.       memset(pongstr, 0, sizeof(pongstr));
  133.       pongptr = pongstr;
  134.  
  135.       signal(SIGALRM, pingTimeout);
  136.       (void)alarm(MAXTIMEOUT);
  137.  
  138.       while(1)
  139.       {
  140.          recv_data((clients[curClient]).sockfd, readbuf, 
  141.                    sizeof(readbuf)-1, 1);
  142.  
  143.          debug("(in sendPings) data = %s\n", readbuf);
  144.  
  145.          if (strstr(readbuf, "PONG") != NULL)
  146.          {
  147.             (void)alarm(0);
  148.             break;
  149.          }
  150.       }
  151.  
  152.       dataptr = strstr(readbuf, "PONG");
  153.       if (dataptr == NULL)
  154.       {
  155.          error("unexpected error.. PONG not in data\n\n");
  156.          quit(ERROR);
  157.       }
  158.  
  159.       dataptr += 5; /* skip "PONG " */
  160.  
  161.       count = 0;
  162.       while ((*dataptr) && (*dataptr != ' ') &&
  163.              (isprint(*dataptr) != 0) && (count < (int)sizeof(pongstr)))
  164.       {
  165.          *pongptr++ = *dataptr++;
  166.          count++;
  167.       }
  168.  
  169.       if (pingval == atoi(pongstr)) continue;
  170.       else
  171.       {
  172.          signal(SIGPIPE, SIG_IGN);
  173.  
  174.          send_data((clients[curClient]).sockfd, "ERROR: %s\n", PINGERROR);
  175.          (void)sleep(MAXPAUSE); /* give them time to react */
  176.  
  177.          res = seteuid(0); 
  178.          if (res == ERROR) 
  179.          {
  180.             error("error with seteuid: %s\n\n", strerror(errno));
  181.             quit(ERROR);
  182.          }
  183.  
  184.          res = kill(getppid(), SIGTERM);
  185.          if (res == ERROR) 
  186.             error("error killing parent process (pid %d): %s\n\n",
  187.                   getppid(), strerror(errno));
  188.  
  189.          res = seteuid(pwd->pw_uid);
  190.          if (res == ERROR) 
  191.          {
  192.             error("error with seteuid: %s\n\n", strerror(errno));
  193.             quit(ERROR);
  194.          }
  195.  
  196.          quit(ERROR);
  197.       }
  198.    }
  199. }
  200.